package com.kkw.shoppingmall.order.config;

import com.kkw.shoppingmall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.ChannelN;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Kevin
 * @date 2021-06-18
 **/
@Configuration
public class MyMQConfig {



    @Bean
    public Exchange orderEventExchange() {
        /**
         *   String name,
         *   boolean durable,
         *   boolean autoDelete,
         *   Map<String, Object> arguments
         */
        return new TopicExchange("order-event-exchange", true, false);
    }

    /**
     * 容器中的Binding Queue Exchange都会自动创建（Rabbitmq没有的情况下)
     * 一旦创建好队列以后，即使队列里面的属性更改了 他也不会重新再次覆盖创建
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        //死信交换机
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        //死信路由键
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        // 消息过期时间 1分钟
        arguments.put("x-message-ttl", 300000);
        Queue orderDelayQueue = new Queue("orderDelayQueue", true, false, false,arguments);

        return orderDelayQueue;
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        Queue orderReleaseOrderQueue = new Queue("orderReleaseOrderQueue", true, false, false);

        return orderReleaseOrderQueue;
    }


    @Bean
    public Binding orderCreateOrderBingDing() {
        /**
         * String destination, 目的地（队列名或者交换机名字）
         * DestinationType destinationType, 目的地类型（Queue、Exhcange）
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
        return new Binding("orderDelayQueue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    public Binding orderReleaseOrderBingDing() {
        return new Binding("orderReleaseOrderQueue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单释放直接和库存释放进行绑定    防止解锁库存的时候因为订单释放时的卡顿而 先进行解锁库存了，查不到订单的状态他就不进行解锁，从而导致这个
     * 订单一直卡在这里
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBingDing() {
        return new Binding("stockReleaseStockQueue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }


    //***====================================================秒杀============================================***//
    @Bean
    public Queue orderSeckillOrderQueue() {
        Queue orderReleaseOrderQueue = new Queue("orderSeckillOrderQueue", true, false, false);

        return orderReleaseOrderQueue;
    }

    @Bean
    public Binding orderSeckillOrderQueueBingDing() {
        return new Binding("orderSeckillOrderQueue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.other",
                null);
    }
}

